package com.today.step.lib.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.today.step.lib.Logger;
import com.today.step.lib.R;
import com.today.step.lib.model.LineChartViewParams;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * Created by shangsong on 17-10-24.
 */

public class StepHistoryChatView extends LinearLayout {
    private final String TAG = this.getClass().getSimpleName();
    private LineChartView lineChartView;

    public StepHistoryChatView(Context context) {
        super(context);
        init(context);
    }

    public StepHistoryChatView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.step_history_chat_view, this);
        lineChartView = (LineChartView) findViewById(R.id.lineChatView);
    }

    public void setData(LineChartViewParams lineChartViewParams) {
        refreshUI(lineChartViewParams);
    }

    private void refreshUI(LineChartViewParams lineChartViewParams) {
        if (null == lineChartViewParams) {
            Logger.d(TAG, "lineChartViewParams is null!");
            return;
        }

        List<Line> lines = new ArrayList<>();
        List<PointValue> pointValues = lineChartViewParams.getPointValueList();
        Line line = new Line(pointValues);
        line.setColor(Color.parseColor(lineChartViewParams.getColorStr()));
        line.setShape(lineChartViewParams.getShape());
        line.setCubic(lineChartViewParams.isCubic());
        line.setFilled(lineChartViewParams.isFilled());
        line.setHasLabels(lineChartViewParams.isHasLabels());
        line.setHasLabelsOnlyForSelected(lineChartViewParams.isHasLabelForSelected());
        line.setHasLines(lineChartViewParams.isHasLines());
        line.setHasPoints(lineChartViewParams.isHasPoints());
        line.setPointColor(Color.parseColor(lineChartViewParams.getPointColorStr()));
        lines.add(line);

        LineChartData lineChartData = new LineChartData(lines);
        if (lineChartViewParams.isHasXAxes()) {
            Axis axisX = new Axis(lineChartViewParams.getXAxisValues());
            axisX.setHasLines(true);
            axisX.setName(lineChartViewParams.getXAxesNames());
//            axisX.setTextSize(10);
            axisX.setHasSeparationLine(true);
            axisX.setLineColor(getContext().getResources().getColor(R.color.color_bbd8ef));
            axisX.setInside(false);
            axisX.setTextColor(getContext().getResources().getColor(android.R.color.white));
            lineChartData.setAxisXBottom(axisX);
        } else {
            lineChartData.setAxisXBottom(null);
        }

        if (lineChartViewParams.isHasYAxes()) {
            Axis axisY = new Axis().setHasLines(false);
            axisY.setMaxLabelChars(5);
            axisY.setHasSeparationLine(false);
            axisY.setName(lineChartViewParams.getYAxesNames());
            axisY.setTextColor(getContext().getResources().getColor(android.R.color.white));
            lineChartData.setAxisYLeft(axisY);
        } else {
            lineChartData.setAxisYLeft(null);
        }

        lineChartData.setBaseValue(Float.NEGATIVE_INFINITY);

        Viewport v = new Viewport(0, lineChartViewParams.getYAxisMaxValue(),
                lineChartViewParams.getXAxisValues().size(), 0);
        lineChartView.setMaximumViewport(v);
        lineChartView.setCurrentViewport(v);
        lineChartView.setZoomEnabled(false);
        lineChartView.setScrollEnabled(false);

        lineChartView.setLineChartData(lineChartData);
    }
}
